//
//  B_SelectGoodsVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/13.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_SelectGoodsVC.h"
#import "B_SelectedGoodsTVC.h"
#import "B_SelecetGoodslistModel.h"
#import "B_GoodsDetailVC.h"

@interface B_SelectGoodsVC ()

@property (nonatomic, strong) BaseTableView * mainView;

@end

@implementation B_SelectGoodsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    [self setTitle:@"选择商品"];
    WeakSelf(weSelf, self);
    [self setRightTextBarButtonItemWithFrame:CGRectZero title:@"完成" titleColor:[UIColor color_text_red] backImage:@"" selectBackImage:@"" action:^(GUIButton *button) {
        //TODO:完成选择
        [weSelf.selectArray removeAllObjects];
        for (B_SelecetGoodslistModel * model in weSelf.dataArray) {
            if (model.isSelect) {
                [weSelf.selectArray addObject:model];
            }
        }
        if (weSelf.selectArray.count == 0) {
            [KeyWindow makeToast:@"请先选择商品"];
            return ;
        }
        if (weSelf.selectBlock) {
            weSelf.selectBlock(weSelf.selectArray);
        }
        [weSelf leftNavigationAction];
    }];
    self.view = self.mainView;
    [self.mainView setTableViewFrameWithTop:0 left:0 right:0 bottom:0];
    [self getData];
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_SelectedGoodsTVC" bundle:nil] forCellReuseIdentifier:@"B_SelectedGoodsTVC"];
        _mainView.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            return 10;
        };
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            view.backgroundColor = [UIColor color_main_background_color];
            return view;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 130;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_SelecetGoodslistModel * model = weSelf.dataArray[indexPath.section];
            B_SelectedGoodsTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_SelectedGoodsTVC" forIndexPath:indexPath];
            if (model.isSelect) {
                cell.B_selectButton.selected = YES;
            } else {
                cell.B_selectButton.selected = NO;
            }
            [cell.B_goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.goodsImg]];
            cell.B_goodsTitle.text = model.goodsName;
            cell.B_goodsPrice.text = [NSString stringWithFormat:@"¥%@", model.goodsPrice];
            cell.B_goodsStock.text = [NSString stringWithFormat:@"库存%@", safeValue1(model.goodsStock)];
            cell.B_goodsSales.text = [NSString stringWithFormat:@"销量%@", safeValue1(model.goodsSalesVolume)];
            cell.B_selectAction = ^(UIButton *btn) {
                model.isSelect = !model.isSelect;
                [tableView reloadData];
            };
                                      
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_SelecetGoodslistModel * model = weSelf.dataArray[indexPath.section];
            B_GoodsDetailVC * vc = [[B_GoodsDetailVC alloc] init];
            vc.B_GoodsId = model.id;
            [weSelf pushNextViewController:vc];
        };
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return 1;
        };
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return weSelf.dataArray.count;
        };
        
    }
    return _mainView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/topic/selectTopicAssociationGoods/%@", getUserId()] parameters:nil isCache:YES succeed:^(id data) {
        [weSelf.dataArray removeAllObjects];
        if ([data[@"code"] isEqualToString:@"20010"]) {
            for (NSDictionary * dicitonary in data[@"values"]) {
                B_SelecetGoodslistModel * model = [B_SelecetGoodslistModel mj_objectWithKeyValues:dicitonary];
                for (B_SelecetGoodslistModel * model1 in weSelf.selectArray) {
                    if ([model1.id isEqualToString:model.id]) {
                        model.isSelect = YES;
                        break;
                    }
                }
                [weSelf.dataArray addObject:model];
                
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"values"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}


@end
